package com.PMSys.Common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
 * 数据库工具类
 */
public class DbUtils {
	//数据库连接对象
	private static Connection conn=null;
	
	/*
	 * 创建连接对象
	 */
	public static Connection getConnection(){
		try {
			//加载驱动
			Class.forName("oracle.jdbc.OracleDriver");
			String url = "jdbc:oracle:thin:localhost:1521:orcl";
			
			String user = "scott";
			String password = "tiger";
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	return conn;
	}
	
	/*
	 * 关闭连接
	 */
	
	public static void closeConnection(){
		try {
			if(conn!= null&&!conn.isClosed()){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 提交事务
	 */	
	public static void commit(){
		try {
			conn.commit();
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 回滚事务
	 */	
	public static void rollback(){
		try {
			conn.rollback();
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
	
